package com.tinshow.web.httpclient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tinshow.config.utils.WebObject;
import com.tinshow.config.utils.WebUtils;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

/**
 * Created by libo4 on 2017/5/27.
 */
public class EchoSearchClient {

    private static long startTime = System.currentTimeMillis();

    public static String getEchoSearch(String value) throws Exception{
        String uri = "http://www.app-echo.com/api/search/sound?keyword="+ URLEncoder.encode(value,"utf-8") +"&page=1&limit=5&src=0";
        Map<String,String> headerMap = new HashMap<String,String>();
        headerMap.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        headerMap.put("Accept-Encoding","gzip, deflate, sdch");
        headerMap.put("Connection","keep-alive");
        headerMap.put("Accept-Language","zh-CN,zh;q=0.8");
        headerMap.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
        GetMethod getMethod = (GetMethod) WebUtils.doGet(uri,headerMap);
        if(getMethod != null){
            String result = getResponseBody4Zip(getMethod);
            if(StringUtils.isNotEmpty(result)){
                parseJson(result);
            }
            getMethod.releaseConnection();

        }
        return null;
    }

    public static String getResponseBody4Zip(GetMethod get){
        StringBuffer sb = new StringBuffer();
        try {
            if(StringUtils.isNotEmpty(new String(get.getResponseBody()))){
                GZIPInputStream gzin;
                if(get.getResponseHeaders("Content-Encoding") != null
                        && get.getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1){
                    InputStream is = get.getResponseBodyAsStream();
                    gzin = new GZIPInputStream(is);
                    InputStreamReader isr = new InputStreamReader(gzin,"utf-8");
                    BufferedReader br = new BufferedReader(isr);
                    String tempbf;
                    while((tempbf = br.readLine()) != null){
                        sb.append(tempbf);
                        sb.append("\r\n");
                    }
                    isr.close();
                    gzin.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String parseJson(String value){
        JSONObject jsonObject = JSON.parseObject(value);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for(int i = 0; i< jsonArray.size();i++){
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            String name = jsonObject1.getString("name");
            String source = jsonObject1.getString("source");
            System.out.print(name + "=================" + source + "\r\n");
        }
        long endTime = System.currentTimeMillis();
        System.out.print(endTime-startTime+"ms");
        return null;
    }

    public static void main(String[] args){
        try{
            getEchoSearch("真的爱你");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
